/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agents;

import ai1.Enviroment;

/**
 *
 * @author orcas
 */
public class AutomatonAgent extends Agent {

    private enum STATE {
        GOING_LEFT, GOING_RIGHT, TURNING, ERROR;  //; is optional
    }
    private STATE state;

    public AutomatonAgent(String name, int row, int col, String orientation) {
        super(name, row, col, orientation);
        state = STATE.GOING_LEFT;
    }

    public String getMove(Enviroment env) {
        String move = null;
        System.out.print("STATE = "+state+", ");
        switch (state) {
            case GOING_LEFT:
                if (env.isCellFree(getRow(), getCol() - 1)) {
                    move = "FWD";
                } else {
                    state = STATE.TURNING;
                    move = "RIGHT";
                }
                break;
            case GOING_RIGHT:
                if (env.isCellFree(getRow(), getCol() + 1)) {
                    move = "FWD";
                } else {
                    state = STATE.TURNING;
                    move = "RIGHT";
                }
                break;
            case TURNING:
                if (getOrientation().equals("North") || getOrientation().equals("South")) {
                    move = "RIGHT"; //keep turning!
                } else { //start going
                    if (getOrientation().equals("West")) {
                        state = STATE.GOING_LEFT;
                    } else if (getOrientation().equals("East")) {
                        state = STATE.GOING_RIGHT;
                    }
                    else state=STATE.ERROR;
                    move = "FWD";
                }
                break;
        }
        System.out.println("move = "+move);
        return move;
    }
}
